unit fCalendar;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus,
  uDAScriptingProvider, uDADataTable, uDAMemDataTable, DB, uDAInterfaces, fClientDataModule,cxStyles,
  cxGraphics, cxEdit, cxScheduler, cxSchedulerStorage,
  cxSchedulerCustomControls, cxSchedulerCustomResourceView, cxSchedulerDayView,
  cxSchedulerDateNavigator, cxSchedulerHolidays, cxSchedulerTimeGridView,
  cxSchedulerUtils, cxSchedulerWeekView, cxSchedulerYearView,
  cxSchedulerGanttView, cxControls, cxSchedulerDBStorage, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinCaramel, dxSkinOffice2010Silver,
  dxSkinSeven,  cxSplitter, VirtualTrees, ExtCtrls, dxSkinscxSchedulerPainter;
type
  TfmCalendar = class(TForm)
    dsBase: TDADataSource;
    tblBase: TDAMemDataTable;
    cxScheduler1: TcxScheduler;
    cxSchedulerDBStorage1: TcxSchedulerDBStorage;
    tblLogin: TDAMemDataTable;
    dsLogin: TDADataSource;
    pnlLeft: TPanel;
    vstUser: TVirtualStringTree;
    Splitter1: TcxSplitter;
    procedure vstUserGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean;
      var ImageIndex: Integer);
    procedure vstUserFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex);
    procedure vstUserGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    function GetNode(AGuid: string): PVirtualNode;
  public
    { Public declarations }
    procedure Initialize(AId: TGuid);
    procedure SelectNode;
  end;

var
  fmCalendar: TfmCalendar;

implementation

//uses  metelese;

uses
  uPublic, tools;

{$R *.dfm}

type
  TUserNodeData = record
    Text: string;
    Id: TGuid;
  end;
  PUserNodeData = ^TUserNodeData;

function TfmCalendar.GetNode(AGuid: string): PVirtualNode;
var
  node: PVirtualNode;
  nd: PUserNodeData;
begin
  node := vstUser.GetFirst;
  while node <> nil do
  begin
    nd := vstUser.GetNodeData(node);
    if GuidToString(nd^.Id) = AGuid then
      Break;
    node := vstUser.GetNext(node);
  end;
  Result := node;
end;  

procedure TfmCalendar.SelectNode;
var
  node : PVirtualNode;
begin
  node := GetNode(GetUserId);
  if node <> nil then
  begin
    vstUser.Selected[node] := True;
    vstUserFocusChanged(vstUser, node,0);
  end;
end;  

procedure TfmCalendar.FormCreate(Sender: TObject);
var
  node: PVirtualNode;
  nd: PUserNodeData;
  sql: string;
begin
  vstUser.NodeDataSize := SizeOf(TUserNodeData);
  try
    sql := 'select buv_uservisible,bu_name,bu_cname from b_user_visible left outer join b_user on buv_uservisible=bu_id where buv_data=1 and buv_uid='+QuotedStr(GetUserId);
//    OpenPublicQuery('select bu_id, bu_name, bu_cname from b_user');
    OpenPublicQuery(sql);
    with ClientDataModule do
    begin
      tblDynamic.First;
      while not tblDynamic.EOF do
      begin
        node := vstUser.AddChild(nil);
        nd := vstUser.GetNodeData(node);
        nd^.Text := tblDynamic.FieldByName('bu_name').AsString + ' (' +tblDynamic.FieldByName('bu_cname').AsString+' )';
        nd^.Id := tblDynamic.FieldByName('buv_uservisible').AsGuid;
        tblDynamic.Next;
      end;
    end;
  finally
    ClosePublicQuery;
  end;
end;

procedure TfmCalendar.Initialize(AId: TGUID);
begin
  tblLogin.Close;
  tblLogin.Where.Clear;
  tblLogin.Where.AddCondition('bu_id', cEqual, QuotedStr(GuidToString(AId)));
  tblLogin.Open;  
//  tblBase.Close;
//  tblBase.Where.Clear;
//  tblBase.Where.AddCondition('dsc_resourceid', cEqual, QuotedStr(GuidToString(AId)));
//  tblBase.Open;
end;
procedure TfmCalendar.vstUserFocusChanged(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex);
var
  nd: PUserNodeData;
begin
  if Node = nil then Exit;
  nd := Sender.GetNodeData(Node);
  Initialize(nd^.Id);
end;

procedure TfmCalendar.vstUserGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
begin
  ImageIndex := 279;
end;

procedure TfmCalendar.vstUserGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);
var
  nd: PUserNodeData;
begin
  nd := Sender.GetNodeData(Node);
  CellText := nd^.Text;
end;

end.
